﻿using System;
using System.Collections.Generic;

using RexToy.UI.WinForm.ViewModel;

namespace RexToy.UI.WinForm.Controls
{
    public class Button : System.Windows.Forms.Button, IBindCommand
    {
        private IUICommand _cmd;
        public IUICommand Command
        {
            get { return _cmd; }
            set
            {
                CommandBindRegistry.Instance().PutItem(this);
                _cmd = value;
            }
        }

        public Button()
        {
            this.Click += new EventHandler(Button_Click);
            this.Disposed += new EventHandler(Button_Disposed);
        }

        void Button_Disposed(object sender, EventArgs e)
        {
            if (this._cmd != null)
                CommandBindRegistry.Instance().RemoveItem(this);
        }

        protected virtual void Button_Click(object sender, EventArgs e)
        {
            if (_cmd != null)
                _cmd.Execute();
        }

        public void QueryCanExecute()
        {
            if (_cmd != null)
                this.Enabled = _cmd.CanExecute();
        }
    }
}
